package com.yikes.srb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.utils.object.BeanUtils;
import com.yikes.srb.leadingModle.form.integralGrade.IntegralGradeAddForm;
import com.yikes.srb.leadingModle.form.integralGrade.IntegralGradeEditForm;
import com.yikes.srb.leadingModle.form.integralGrade.IntegralGradeQuery;
import com.yikes.srb.leadingModle.vo.IntegralGradeVO;
import com.yikes.srb.pojo.entity.IntegralGrade;
import com.yikes.srb.mapper.IntegralGradeMapper;
import com.yikes.srb.service.IntegralGradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {

    @Override
    public PageResult<IntegralGradeVO> listPage(IntegralGradeQuery query) {

        Page<IntegralGrade> page = lambdaQuery().ge(ObjectUtil.isNotNull(query.getIntegralStart()), IntegralGrade::getIntegralStart, query.getIntegralStart())
                .le(ObjectUtil.isNotNull(query.getIntegralEnd()), IntegralGrade::getIntegralEnd, query.getIntegralEnd())
                .eq(ObjectUtil.isNotNull(query.getBorrowAmount()), IntegralGrade::getBorrowAmount, query.getBorrowAmount())
                .page(Page.of(query.getPageNum(), query.getPageSize()));

        List<IntegralGradeVO> bean = BeanUtils.toBean(page.getRecords(), IntegralGradeVO.class);
        return PageResult.build(page.getTotal(), page.getPages(), bean);
    }

    @Override
    public IntegralGradeVO infoId(Long id) {

        IntegralGrade integralGrade = getById(id);
        IntegralGradeVO bean = BeanUtils.toBean(integralGrade, IntegralGradeVO.class);
        return bean;
    }

    @Override
    public void create(IntegralGradeAddForm form) {

        IntegralGrade bean = BeanUtils.toBean(form, IntegralGrade.class);
        save(bean);
    }

    @Override
    public void edit(IntegralGradeEditForm form) {

        IntegralGrade bean = BeanUtils.toBean(form, IntegralGrade.class);
        updateById(bean);

    }

    @Override
    public void del(Long id) {

        baseMapper.deleteById(id);
    }
}
